package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.common.Constants;
import cn.edu.seu.ssny.common.R;
import cn.edu.seu.ssny.common.utils.DateUtils;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 能效分析
 */
@RestController
@RequestMapping(value = {"/analysis/"})
public class DataAnalysisController extends BaseController {

    @Autowired
    private IStatisticsService statisticsService;

    /**
     * 功能描述:设备耗电量占比
     * <对于指定项目，返回指定设备类型分类的每个设备类型能耗
     *
     * @param session
     * @return
     */
    @GetMapping("/powercost")
    public R getPowercostByProject(HttpSession session) {
        Project project = getCurrProject(session);
        Map<String, Double> equipmetsAndPowercosts = statisticsService.caculatePowercostByProject(project);
        List<String> types = new ArrayList<>();
        List<Double> powerCosts = new ArrayList<>();
        for (Map.Entry<String, Double> item : equipmetsAndPowercosts.entrySet()) {
            types.add(item.getKey());
            powerCosts.add(item.getValue());
        }
        Map<String, Object> data = new HashMap<>();
        data.put("types", types);
        data.put("powercosts", powerCosts);
        return R.ok().data(data);
    }

    /**
     * 功能描述:机组能效
     * <返回机组能效能效(指定项目)(机组能效)
     *
     * @param session
     * @param timeMode
     * @return
     */
    @GetMapping("/machine_group_efficiency")
    public R getMachineGroupEfficiency(HttpSession session, @RequestParam("timeMode") int timeMode, @RequestParam(value = "baseYear", required = false) String baseYear, @RequestParam(value = "timeLength", defaultValue = "7", required = false) int timeLength) throws MissingServletRequestParameterException {
        if (timeMode == DateUtils.MODE_YEAR && baseYear == null) {
            throw new MissingServletRequestParameterException("baseYear", "String");
        }
        checkTimeMode(timeMode);
        Project project = getCurrProject(session);
        List<Date> timeList = DateUtils.getDateList(timeMode, timeLength);
        Map<String, List<Double>> machineGroupEfficiencies = statisticsService.caculateMachineGroupEfficiencyByProject(project, timeList, timeMode);
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map.Entry<String, List<Double>> item : machineGroupEfficiencies.entrySet()) {
            Map<String, Object> map = new HashMap();
            map.put("name", item.getKey());
            map.put("efficiency", item.getValue());
            list.add(map);
        }
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap();
        data.put("dates", dates);
        data.put("machineGroup", list);
        return R.ok().data(data);
    }

    /**
     * 功能描述:系统能效
     * <返回制冷能效或者供暖能效(指定项目)(系统能效)
     * <在 HOUR DAY MONTH模式下不需要baseYear参数
     * <在 YEAR模式下需要baseYear参数 baseYear参数形式为年份(2020)
     *
     * @param session
     * @param timeMode
     * @return
     */
    @GetMapping("/system_efficiency")
    public R getSystemEfficiency(HttpSession session, @RequestParam("timeMode") int timeMode, @RequestParam(value = "baseYear", required = false) String baseYear, @RequestParam(value = "timeLength", defaultValue = "7", required = false) int timeLength) throws MissingServletRequestParameterException {
        if (timeMode == DateUtils.MODE_YEAR && baseYear == null) {
            throw new MissingServletRequestParameterException("baseYear", "String");
        }
        checkTimeMode(timeMode);
        Project project = getCurrProject(session);

        List<Date> timeList = DateUtils.getDateList(timeMode, timeLength, baseYear);
        List<Double> coolingEfficiency = statisticsService.caculateSystemEfficiency(project, timeList, Constants.CoolingMode, timeMode);
        List<Double> heatingEfficiency = statisticsService.caculateSystemEfficiency(project, timeList, Constants.HeatingMode, timeMode);
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap();
        data.put("dates", dates);
        data.put("coolingEfficiency", coolingEfficiency);
        data.put("heatingEfficiency", heatingEfficiency);
        return R.ok().data(data);
    }

    /**
     * 功能描述:系统能耗指标
     * <mode=0:制冷模式
     * <mode=1:供暖模式
     *
     * @param session
     * @param mode
     * @return
     */
    @GetMapping("/system_powercost_std")
    public R getSystemPowercostStd(HttpSession session, @RequestParam("mode") Integer mode, @RequestParam(value = "timeLength", defaultValue = "7", required = false) int timeLength) {
        Project project = getCurrProject(session);
        List<Date> timeList = DateUtils.getDateList(DateUtils.MODE_DAY, timeLength);
        List<Double> systemPowercostStd = statisticsService.caculateSystemPowercostStd(project, timeList, DateUtils.MODE_DAY, mode);
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap<>();
        data.put("dates", dates);
        data.put("systemPowercostStd", systemPowercostStd);
        return R.ok().data(data);
    }
}
